package xyz.gpgl.erp.logic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.util.base.CommonUtil;
import common.util.base.Result;
import common.util.mybatis.plus.layui.Page;
import common.util.mybatis.plus.logic.LogicBaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.gpgl.erp.logic.entity.LogicEmailInfo;
import xyz.gpgl.erp.logic.service.ILogicEmailInfoService;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liubo
 * @since 2021-04-21
 */
@Slf4j
@Controller
@RequestMapping("/logic/logic_email_info")
public class LogicEmailInfoController extends LogicBaseController<ILogicEmailInfoService, LogicEmailInfo>{

    @Autowired
    private ILogicEmailInfoService iLogicEmailInfoService;

    /**
     * 跳转的列表页面
     * @return 跳转页面
     */
    @RequestMapping("/listPage")
    public Object listPage() {
      return "logicEmailInfo_list";
    }

    /**
     * 列表页面表格数据
     * @param entity 实体对象
     * @param page 分页对象
     * @return page 分页对象
     */
    @ResponseBody
    @RequestMapping("/listData")
    public Object listData(LogicEmailInfo entity, Page<LogicEmailInfo> page) {
        QueryWrapper<LogicEmailInfo> query= new QueryWrapper<>();
        if(CommonUtil.isNotEmpty(entity)){
            Map<String,Object> entityMap = CommonUtil.convertBeanToMap(entity);
            for (String key:entityMap.keySet()){
                if(CommonUtil.isNotEmpty(entityMap.get(key))){
                    query.like(key,entityMap.get(key));
                }
            }
        }
        return Result.success(iLogicEmailInfoService.page(page, query));
    }

    /**
     * 用去当前用户没有选中的用户
     * @param sharesId
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping("/notChecklistData")
    public Result notChecklistData(Integer sharesId, Page<LogicEmailInfo> page) {
        return Result.success(iLogicEmailInfoService.notChecklistData(sharesId,page));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/editPage")
    public Object editPage(String id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            LogicEmailInfo entity = this.iLogicEmailInfoService.getById(id);
            if(CommonUtil.isNotEmpty(entity)) {
                model.addAttribute("entity",entity);
            }
        }
        return "logicEmailInfo_edit";
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/save")
    public Object save(@RequestBody LogicEmailInfo entity) {
        log.info("开始保存数据:{}" ,entity);
        this.iLogicEmailInfoService.saveOrUpdate(entity);
        return Result.success();
    }

    /**
     * 数据删除
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/removed")
    public Object removed(LogicEmailInfo entity) {
        this.iLogicEmailInfoService.removeById(entity.getId());
        return Result.success();
    }

}
